package com.ctshk.rpc.order.goods.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ctshk.rpc.order.goods.dto.OrderCommodityRefundDTO;
import com.ctshk.rpc.order.goods.entity.OrderCommodityRefund;
import com.ctshk.rpc.order.goods.req.CommodityOrderRefundReq;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商品退款记录 Mapper 接口
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-19
 */
public interface OrderCommodityRefundMapper extends BaseMapper<OrderCommodityRefund> {

    @Select({
            "<script>",
            "SELECT ",
            "t.id,t.refund_number,t.standard_amount,t.refund_amount,t.refund_way_id,t.refund_way," +
                    "t.refund_account,t.refund_reason,t.remark,t.gmt_create,t.create_id,t.status,t.reject_reason," +
                    "t.currency_id,t.exchange_rate,t.currency ",
            "FROM `ctshk-goods-order`.t_order_commodity_refund t ",
            "WHERE t.is_deleted = 0 AND t.order_id = #{orderId} ",
            "ORDER BY t.gmt_create DESC",
            "</script>"
    })
    List<OrderCommodityRefundDTO> findOrderCommodityRefundByOrderId(@Param("orderId") Long orderId);

    /**
     * 根据条件查询商品订单退款明细
     * @param req
     * @return
     */
    @Select("<script>SELECT a.*\n" +
            "FROM `ctshk-goods-order`.t_order_commodity_refund a\n" +
            "WHERE a.is_deleted = 0\n" +
            "<if test='req.orderId != null'>AND a.order_id = #{req.orderId}</if>" +
            "ORDER BY a.gmt_modified DESC</script>")
    List<OrderCommodityRefund> queryRefundListByCondition(@Param("req") CommodityOrderRefundReq req);

    /**
     * 根据订单id查询订单退款记录
     * @param id
     * @return
     */
    @Select({
            "SELECT * FROM `ctshk-goods-order`.t_order_commodity_refund WHERE order_id = #{id}"
    })
    OrderCommodityRefund selectByOrderId(@Param("id") Long id);

    /**
     * 查询订单商品退款记录列表
     * @param orderId
     * @return
     */
    @Select({
            "SELECT * FROM `ctshk-goods-order`.t_order_commodity_refund WHERE order_id = #{orderId}"
    })
    List<OrderCommodityRefund> selectListByOrderId(@Param("orderId") Long orderId);

    /**
     * 新增退款记录
     * @param orderCommodityRefund
     */
    @Insert({
            "<script>" +
                    "INSERT INTO `ctshk-goods-order`.t_order_commodity_refund  " +
                    "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
                    "<if test='id != null'>id, </if>" +
                    "<if test='orderId != null'>order_id, </if>" +
                    "<if test='currencyId != null'>currency_id, </if>" +
                    "<if test='exchangeRate != null'>exchange_rate, </if>" +
                    "<if test='currency != null'>currency, </if>" +
                    "<if test='standardAmount != null'>standard_amount, </if>" +
                    "<if test='refundNumber != null'>refund_number, </if>" +
                    "<if test='refundAmount != null'>refund_amount, </if>" +
                    "<if test='refundWayId != null'>refund_way_id, </if>" +
                    "<if test='refundWay != null'>refund_way, </if>" +
                    "<if test='refundAccount != null'>refund_account, </if>" +
                    "<if test='refundReason != null'>refund_reason, </if>" +
                    "<if test='remark != null'>remark, </if>" +
                    "<if test='status != null'>status, </if>" +
                    "<if test='rejectReason != null'>reject_reason, </if>" +
                    "<if test='createId != null'>create_id, </if>" +
                    "<if test='modifiedId != null'>modified_id, </if>" +
                    "<if test='isDeleted != null'>is_deleted, </if>" +
                    "<if test='gmtCreate != null'>gmt_create, </if>" +
                    "<if test='gmtModified != null'>gmt_modified,</if>" +
                    "</trim>" +
                    "VALUES " +
                    "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
                    "<if test='id != null'>#{id}, </if>" +
                    "<if test='orderId != null'>#{orderId}, </if>" +
                    "<if test='currencyId != null'>#{currencyId}, </if>" +
                    "<if test='exchangeRate != null'>#{exchangeRate}, </if>" +
                    "<if test='currency != null'>#{currency}, </if>" +
                    "<if test='standardAmount != null'>#{standardAmount}, </if>" +
                    "<if test='refundNumber != null'> #{refundNumber}, </if>" +
                    "<if test='refundAmount != null'>#{refundAmount}, </if>" +
                    "<if test='refundWayId != null'>#{refundWayId}, </if>" +
                    "<if test='refundWay != null'>#{refundWay}, </if>" +
                    "<if test='refundAccount != null'>#{refundAccount}, </if>" +
                    "<if test='refundReason != null'>#{refundReason}, </if>" +
                    "<if test='remark != null'>#{remark}, </if>" +
                    "<if test='status != null'>#{status}, </if>" +
                    "<if test='rejectReason != null'>#{rejectReason}, </if>" +
                    "<if test='createId != null'>#{createId}, </if>" +
                    "<if test='modifiedId != null'>#{modifiedId}, </if>" +
                    "<if test='isDeleted != null'>#{isDeleted}, </if>" +
                    "<if test='gmtCreate != null'>#{gmtCreate}, </if>" +
                    "<if test='gmtModified != null'>#{gmtModified},</if>" +
                    "</trim>"+
                    "</script>"
    })
    void insertRefund(OrderCommodityRefund orderCommodityRefund);

}
